import numpy as np
import numba as nb
from numba import cuda
import math
import operator
from ..utils import get_grid_1d, flatten


@cuda.jit()
def _gpu_divide(a: np.ndarray, b: np.ndarray, c: np.ndarray):
    x = cuda.grid(1)
    if x < a.shape[0]:
        c[x] = operator.truediv(a[x], b[x])


def divide(a: np.ndarray, b: np.ndarray):
    a, b = np.broadcast_arrays(a, b)

    c = np.empty_like(a)

    grid, block = get_grid_1d(a.size)
    _gpu_divide[grid, block](flatten(a), flatten(b), flatten(c))

    return c
